﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCT.Data;
using DCT.Entities;
using System.IO;

public partial class Admin_TuyenDung_QLTuyenDung : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        try
        {
            DCT.Entities.TList<DCT.Entities.TuyenDung> Tlist_TuyenDung = DataRepository.TuyenDungProvider.GetAll();
            gridCatalog.DataSource = Tlist_TuyenDung;

            gridCatalog.DataBind();
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(Pager1.CurrentIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLTuyenDung.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }
    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        using (DCT.Entities.TuyenDung entTuyenDung = new DCT.Entities.TuyenDung())
        {

            entTuyenDung.Id = Convert.ToInt32(H_CatalogId.Value);
            entTuyenDung.TieuDe = Common.StandardString(txtTieuDe.Text);
            entTuyenDung.NoiDung = Server.HtmlDecode(txtNoiDung.Text);
            entTuyenDung.TepTin =Common.ConvertToUnSign(lblFileDinhKem.Text);
            entTuyenDung.NgayCapNhat = Common.GetServerDateTime();

            bool updateStatus = false;
            try
            {
                if (entTuyenDung.Id == 0)
                {
                    updateStatus = DataRepository.TuyenDungProvider.Insert(entTuyenDung);
                }
                else
                {
                    entTuyenDung.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.TuyenDungProvider.Update(entTuyenDung);
                }
                if (!updateStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TUYENDUNG_CAPNHAT_THATBAI, true);
                }
                else
                {
                    H_CatalogId.Value = "0";
                    txtTieuDe.Text = "";
                    txtNoiDung.Text = "";
                    lblFileDinhKem.Text = "";
                    Bind_Data(Pager1.CurrentIndex);
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.TUYENDUNG_CAPNHAT_THANHCONG, true);
                }
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TUYENDUNG_CAPNHAT_THATBAI, true);
            }
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        txtTieuDe.Text = "";
        txtNoiDung.Text = "";
        lblFileDinhKem.Text = "";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");
        }
    }
    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind_Data(Pager1.CurrentIndex);
        H_CatalogId.Value = "0";
        txtTieuDe.Text = "";
        txtNoiDung.Text = "";
        lblFileDinhKem.Text = "";
    }
    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            try
            {
                int Id = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.TuyenDungProvider.Delete(Id);
                if (!deleteStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TUYENDUNG_XOA_THATBAI, true);
                }
                else
                {
                    Bind_Data(Pager1.CurrentIndex);
                    H_CatalogId.Value = "0";
                    txtTieuDe.Text = "";
                    txtNoiDung.Text = "";
                    lblFileDinhKem.Text = "";
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.TUYENDUNG_XOA_THANHCONG, true);
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLTuyenDung.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TUYENDUNG_XOA_THATBAI, true);
                return;
            }
        }
        if (e.CommandName == "Select")
        {
            try
            {
                int Id = Convert.ToInt32(e.CommandArgument.ToString());
                using (TuyenDung entTuyenDung = DataRepository.TuyenDungProvider.GetById(Id))
                {
                    if (entTuyenDung != null)
                    {

                        H_CatalogId.Value = entTuyenDung.Id.ToString();
                        H_FileName.Value = entTuyenDung.TepTin;
                        txtTieuDe.Text = entTuyenDung.TieuDe;
                        txtNoiDung.Text = entTuyenDung.NoiDung;
                        lblFileDinhKem.Text = entTuyenDung.TepTin;
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
    protected void UploadFile_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
    {
        string sTuyenDungPath = Server.MapPath(Common.sPathUpFile);
        if (!Directory.Exists(sTuyenDungPath)) Directory.CreateDirectory(sTuyenDungPath);
        if (File.Exists(sTuyenDungPath + "\\" + args.FileName))
        {
            File.Delete(sTuyenDungPath + "\\" + args.FileName);
        }
        if (File.Exists(sTuyenDungPath + "\\" + H_FileChonLai.Value))
        {
            File.Delete(sTuyenDungPath + "\\" + H_FileChonLai.Value);
        }
        args.MoveTo(sTuyenDungPath + "\\" + args.FileName);
        H_FileName.Value = args.FileName;
        lblFileDinhKem.Text = args.FileName;
    }

    protected void UploadFile_AttachmentRemoveClicked(object sender, CuteWebUI.AttachmentItemEventArgs args)
    {
        string sTuyenDungPath = Server.MapPath(Common.sPathUpFile);
        if (File.Exists(sTuyenDungPath + "\\" + args.Item.FileName))
        {
            File.Delete(sTuyenDungPath + "\\" + args.Item.FileName);
        }
        H_FileName.Value = "";
    }

}